﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using ExcelHandler;
using System.IO;

namespace TokenGenerator
{
    class Program
    {
        static void Main(string[] args)
        {
            ExcelManager manager = new ExcelManager();
            manager.OpenWorkbook(@"E:\PostSystem.xlsx");
            manager.OpenWorksheet(1);
            var data = manager.GetAllData();
            var writer = new StreamWriter(@"E:\1.txt");
            for (int i = 0; i < data.Length; i++)
            {
                var item = data[i];
                var sno = Convert.ToInt64(item[0]);
                var time = GetTimeStamp();
                var password = SHA256Hash($"{sno}.{time}");
                var email = item[2];
                writer.WriteLine($"{sno}-{password}-{email}");
            }
            writer.Dispose();
            writer.Close();
            ExcelManager.Close();
        }

        public static string SHA256Hash(string str)
        {
            byte[] data = Encoding.UTF8.GetBytes(str);
            SHA256 shaM = new SHA256Managed();
            var hashBytes = shaM.ComputeHash(data);
            return Convert.ToBase64String(hashBytes);
        }

        /// <summary>
        /// 获取时间戳
        /// </summary>
        /// <returns></returns>
        public static long GetTimeStamp()
        {
            TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToInt64(ts.TotalSeconds);
        }
    }
}
